package com.younion.dao;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.CollectionUtils;

import com.younion.common.base.BaseDAO;
import com.younion.common.exception.BaseException;
import com.younion.common.paginator.Paginator;
import com.younion.vo.*;

 
public class RechargeRuleDAO extends BaseDAO {

	protected static final Log logger = LogFactory
			.getLog(RechargeRuleDAO.class);

	 
	public Collection selectAll() throws BaseException {
		return queryForList("t_Recharge_Rule.selectAll", null);
	}

	 
	public Collection selectByMap(Map paramMap) throws BaseException {
		if (paramMap == null)
			return null;
		try {
			return queryForList("t_Recharge_Rule.selectByMap", paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					" RechargeRuleDAO，调用selectByMap(Map paramMap)方法出错");
		}
	}

	 
	public int selectCountByMap(Map paramMap) throws BaseException {
		if (paramMap == null)
			return 0;
		try {
			return queryCount("t_Recharge_Rule.selectByMapCount", paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					" RechargeRuleDAO，调用selectByMap(Map paramMap)方法出错");
		}
	}

	 
	public List selectForPage(Map condition, Paginator paginator)
			throws BaseException {
		if (condition == null)
			return null;
		int pageIndex = paginator.getStartIndex();
		int perPageSize = paginator.getPageSize();
		String sqlStr = "";
		try {
			Map vo = (Map) condition.get("paramMap");
			if (vo == null) {
				sqlStr = "t_Recharge_Rule.selectAll";
			} else {
				sqlStr = "t_Recharge_Rule.selectByMap";
			}
			return queryForPage(sqlStr, vo, pageIndex, perPageSize);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					" RechargeRuleDAO，调用selectForPage（Map condition）方法出错");
		}
	}

	 
	public RechargeRuleVO insertByVo(RechargeRuleVO originalVo)
			throws BaseException {
		if (originalVo == null)
			return null;
		try {
			return (RechargeRuleVO) this.insert("t_Recharge_Rule.insertByVo",
					originalVo);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					" RechargeRuleDAO，调用insertByVo(ServiceInitVO originalVo)方法出错");
		}
	}

	 
	public RechargeRuleVO[] insertVos(RechargeRuleVO[] originalVo)
			throws BaseException {
		if (originalVo == null)
			return null;
		try {
			for (int i = 0; i < originalVo.length; i++)
				originalVo[i] = insertByVo(originalVo[i]);
			return originalVo;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					" RechargeRuleDAO，调用insertVos（ServiceInitVO[] originalVo）方法出错");
		}
	}

	 
	public Collection insertByList(Collection originalVo) throws BaseException {
		if (originalVo == null)
			return null;
		try {
			Iterator it = originalVo.iterator();
			while (it.hasNext()) {
				RechargeRuleVO data = (RechargeRuleVO) it.next();
				data = insertByVo(data);
			}
			return originalVo;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					" RechargeRuleDAO，调用insertByList(Collection originalVo)方法出错");
		}
	}

	 
	public boolean updateByMap(Map paramMap) throws BaseException {
		if (paramMap == null)
			return false;
		try {
			this.update("t_Recharge_Rule.updateByMap", paramMap);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					" RechargeRuleDAO，调用updateByMap(Map paramMap)方法出错");
		}
	}

	 
	public boolean updateVos(Map[] originalVo) throws BaseException {
		if (originalVo == null)
			return false;
		try {
			for (int i = 0; i < originalVo.length; i++) {
				if (!updateByMap(originalVo[i]))
					return false;
			}
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					" RechargeRuleDAO，调用updateVos(Map[] originalVo)方法出错");
		}
	}

	 
	public boolean updateByList(Collection originalVo) throws BaseException {
		if (originalVo == null)
			return false;
		try {
			Iterator it = originalVo.iterator();
			while (it.hasNext()) {
				Map data = (Map) it.next();
				if (!updateByMap(data))
					return false;
			}
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					" RechargeRuleDAO，调用updateByList(Collection originalVo)方法出错");
		}
	}

	 
	public boolean deleteAll() throws BaseException {
		try {
			return this.getSqlMapClientTemplate().delete(
					"t_Recharge_Rule.deleteAll", null) > 0 ? true : false;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

	 
	public RechargeRuleVO selectById(Map paramMap) throws BaseException {
		if (paramMap == null || paramMap.isEmpty())
			return null;
		try {
			return (RechargeRuleVO) this.getSqlMapClientTemplate()
					.queryForObject("t_Recharge_Rule.selectById", paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					" RechargeRuleDAO，调用selectById(Map paramMap)方法出错");
		}
	}

	 
	public boolean deleteById(Map paramMap) throws BaseException {
		if (paramMap == null || paramMap.isEmpty())
			return false;
		try {
			this.getSqlMapClientTemplate().delete("t_Recharge_Rule.deleteById",
					paramMap);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					" RechargeRuleDAO，调用deleteById(Map paramMap)方法出错");
		}
	}

	 
	public boolean deleteByIds(List list) throws BaseException {
		if (CollectionUtils.isEmpty(list))
			return false;
		try {
			this.getSqlMapClientTemplate().delete(
					"t_Recharge_Rule.deleteByIds", list);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					" RechargeRuleDAO，调用deleteByIds(List list)方法出错");
		}
	}

	 
	public boolean deleteByMap(Map map) throws BaseException {
		if (CollectionUtils.isEmpty(map))
			return false;
		try {
			this.getSqlMapClientTemplate().delete(
					"t_Recharge_Rule.deleteByMap", map);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					" RechargeRuleDAO，调用deleteByMap(Map paramMap)方法出错");
		}
	}

	public int saveReturnKey(RechargeRuleVO rechargeRuleVO)
			throws BaseException {
		try {
			return Integer
					.parseInt(this.getSqlMapClientTemplate().insert(
							"t_Recharge_Rule.saveReturnKey", rechargeRuleVO)
							.toString());
		} catch (Exception e) {
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

}